import type { RouteRecordRaw } from "vue-router";
import router from "../router/index";

type IRoute = RouteRecordRaw & { id?: string };

// 定义全部路由：Admin下的子一级。
const allRoutes: IRoute[] = [
    {
        id: "1",
        name: "/",
        path: "",
        redirect: "/index/Home"
    },
    {
        id: "1",
        name: "Home",
        path: "Home",
        component: () => import("../views/HomePage/Home/Home.vue")
    },
    {
        id: "2-1",
        name: "GoodsList",
        path: "GoodsList",
        component: () => import("../views/HomePage/goods/GoodsList.vue")
    },
    {
        id: "2-2",
        name: "QueryList",
        path: "QueryList",
        component: () => import("../views/HomePage/goods/QueryList.vue")
    },
    {
        id: "3-1",
        name: "list",
        path: "list",
        component: () => import("../views/HomePage/Banner/list.vue")
    },
    {
        id: "3-2",
        name: "Add",
        path: "Add",
        component: () => import("../views/HomePage/Banner/Add.vue")
    },
    {
        id: "4-1",
        name: "operator",
        path: "operator",
        component: () => import("../views/HomePage/Admin/operator.vue")
    },
    {
        id: "4-2",
        name: "Addoperator",
        path: "Addoperator",
        component: () => import("../views/HomePage/Admin/Addoperator.vue")
    },
]



export default function getRoutes() {
    // 1、获取当前用户的权限
    let checkedKeys: string[] = [];
    const grants = sessionStorage.getItem("grants");
    if (grants) {
        checkedKeys = JSON.parse(grants);
    }
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '3', '3-1', '3-2', '4', '4-1', '4-2','3-1-R','3-1-U','4-1-U','4-1-D','3-1-D'];
    }

    // 2、获取当前用户的路由配置

    allRoutes.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            let currRoute = { ...item }

            let arr = ['0', '0', '0', '0']     // 把crud状态存进数组

            if (checkedKeys.includes(item.id + '-C')) {
                arr[0] = '1'
            }
            if (checkedKeys.includes(item.id + '-R')) {
                arr[1] = '1'
            }
            if (checkedKeys.includes(item.id + '-U')) {
                arr[2] = '1'
            }
            if (checkedKeys.includes(item.id + '-D')) {
                arr[3] = '1'
            }

            currRoute.meta = {
                crud: arr.join('')
            }

            router.addRoute("index", currRoute);
        }
    })

}

export const clearCurrentRoutes = () => {
    // 1、获取当前用户的权限
    let checkedKeys: string[] = [];
    const grants = sessionStorage.getItem("grants");
    if (grants) {
        checkedKeys = JSON.parse(grants);
    }
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '3', '3-1', '3-2', '4', '4-1', '4-2'];
    }

    // 2、删除当前用户的路由配置

    allRoutes.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            //    router.addRoute("Admin",{...item});
            router.removeRoute(item.name as string);
        }
    })

}